const sys = require(`./system.js`)
const $ = sys.$('助学账户信息')
const localStorage = 'localStorage-2'

const task = 'task:all'
sys.run($, task, (resList, cookie, index, username, password, item) => {
    return new Promise(async resolve => {
        // 请求接口
        const info = await $.publicJS.userInfo($, cookie)
        const boaInfo = await $.publicJS.boaInfo($, cookie)
        let couponsList = await $.publicJS.couponsList($, cookie)
        let summary = await $.publicJS.summary($, cookie)
        let transferList = await $.publicJS.transferList2($, cookie, 2)
        let amount1 = summary.items.totalAmount
        if(transferList.total) {
            amount1 += transferList.total.amount
        }

        // 构建 obj 信息
        resList.push({
            ...item,
            "账户全部资产": info.userUserCurrentAmount,
            "账户余额": info.accountUsableAmount,
            '可用盲盒': boaInfo.myChance,
            '可用优惠卷': couponsList.length,
            couponsList: couponsList.map(it => `${it.useCondition} - ${it.amount}`),
            '已购买金额': amount1
        })
        resolve(resList)
    })
}, (resList) => {
    console.log('任务结束 resList:', resList)
    
    if (resList && resList.length > 0) {
        resList = resList.sort((a1, a2) => {
            return a1['序号'] - a2['序号']
        })
        // 存储列表
        $.localStorage.set(task, resList, localStorage)

        let sum = 0
        resList.forEach(item => {
            sum += item['账户全部资产']
        })
        console.log('总资产', sum)
    }
}, 500)
